<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>Flutter实现多主题 | 早起的年轻人</title>
    <meta property="og:title" content="Flutter实现多主题 - 早起的年轻人">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-08-21T20:34:32&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-08-21T20:34:32&#43;08:00'>
        
    <meta name="Keywords" content="Flutter，golang,go语言,go语言笔记,飞雪无情,java,android,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="Flutter实现多主题">
        
    <meta name="author" content="luckly">
    <meta property="og:url" content="https://luckly.work/post/flutter_tips/Flutter%E5%AE%9E%E7%8E%B0%E5%A4%9A%E4%B8%BB%E4%BD%93/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>

<link rel="stylesheet"
      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://luckly.work/">
                        早起的年轻人
                    </a>
                
                <p class="description">专注于Flutter、Android、Java、Go语言(golang)、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://luckly.work/">首页</a>
                    
                    <a  href="https://luckly.work/categories/" title="分类">分类</a>
                    
                    <a  href="https://luckly.work/tags/" title="标签">标签</a>
                    
                    <a  href="https://luckly.work/archives/" title="归档">归档</a>
                    
                    <a  href="https://luckly.work/about/" title="关于我">关于我</a>
                    
                    <a  href="https://github.com/ITmxs/" title="github">github</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#maindart"><strong>main.dart</strong></a></li>
    <li><a href="#themesdart"><strong>themes.dart</strong></a></li>
  </ul>

  <ul>
    <li><a href="#maindart-1">main.dart</a></li>
    <li><a href="#provider文件夹">provider文件夹</a></li>
    <li><a href="#page文件夹">page文件夹</a>
      <ul>
        <li><a href="#home_pagedart"><strong>home_page.dart</strong></a></li>
        <li><a href="#navigationbar_widgetdart"><strong>navigationbar_widget.dart</strong></a></li>
        <li><a href="#profile_widgetdart"><strong>profile_widget.dart</strong></a></li>
      </ul>
    </li>
    <li><a href="#pubspecyaml"><strong>pubspec.yaml</strong></a></li>
  </ul>

  <ul>
    <li><a href="#maindart-2"><strong>main.dart</strong></a></li>
    <li><a href="#listviewdetailsscreendart"><strong>Listviewdetailsscreen.dart</strong></a></li>
    <li><a href="#homescreendart"><strong>homescreen.dart</strong></a></li>
    <li><a href="#carservicesdart"><strong>carservices.dart</strong></a></li>
  </ul>

  <ul>
    <li><a href="#设置和堆叠主题">设置和堆叠主题</a></li>
    <li><a href="#用法">用法</a>
      <ul>
        <li><a href="#主题生成器">主题生成器</a></li>
        <li><a href="#提供多个主题">提供多个主题</a></li>
        <li><a href="#light-和-dark-主题">Light 和 Dark 主题</a></li>
        <li><a href="#更改状态栏颜色">更改状态栏颜色</a></li>
        <li><a href="#主题持久化">主题持久化]</a></li>
      </ul>
    </li>
    <li><a href="#使用主题">使用主题</a>
      <ul>
        <li><a href="#业务逻辑中的访问">业务逻辑中的访问</a></li>
      </ul>
    </li>
  </ul>

  <ul>
    <li><a href="#maindart-3">main.dart</a></li>
    <li><a href="#home_pagedart-1"><strong>home_page.dart</strong></a></li>
    <li><a href="#theme_providerdart"><strong>theme_provider.dart</strong></a></li>
    <li><a href="#z_animated_toggledart"><strong>z_animated_toggle.dart</strong></a></li>
    <li><a href="#pubspecyaml-1"><strong>pubspec.yaml</strong></a></li>
  </ul>

  <ul>
    <li><a href="#this-article-consists-of-two-parts">This article consists of two parts</a></li>
    <li><a href="#本文由两部分组成">本文由两部分组成</a></li>
  </ul>

  <ul>
    <li><a href="#第-1-步将包添加到您的-pubspecyaml-文件中">第 1 步：将包添加到您的 pubspec.yaml 文件中</a></li>
    <li><a href="#第-2-步创建明暗主题">第 2 步：创建明暗主题</a></li>
    <li><a href="#第-3-步将-materialapp-更改为-getmaterialapp-并添加主题">第 3 步：将 MaterialApp 更改为 GetMaterialApp 并添加主题</a></li>
    <li><a href="#第-4-步从主题中获取您的风格">第 4 步：从主题中获取您的风格</a></li>
    <li><a href="#第-5-步动态更改主题">第 5 步：动态更改主题</a></li>
  </ul>

  <ul>
    <li><a href="#第-1-步将包添加到您的-pubspecyaml-文件中-1">第 1 步：将包添加到您的 pubspec.yaml 文件中</a></li>
    <li><a href="#第-2-步创建-themeservice">第 2 步：创建 ThemeService</a></li>
    <li><a href="#第-3-步初始化-getstorage-并从-themeservice-获取-thememode">第 3 步：初始化 GetStorage 并从 ThemeService 获取 ThemeMode</a></li>
    <li><a href="#第-4-步将-themeserviceswitchtheme-添加到按钮的-onpressed-方法中">第 4 步：将 ThemeService().switchTheme 添加到按钮的 onPressed 方法中</a></li>
    <li><a href="#example-project">Example Project</a></li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">Flutter实现多主题</h1>
        </header>
        <date class="post-meta meta-date">
            2021年8月21日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/Flutter'>Flutter</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <blockquote>
<p>本文主要介绍</p>
</blockquote>
<p>getx</p>
<p><a href="https://medium.com/flutter-community/the-flutter-getx-ecosystem-dependency-injection-8e763d0ec6b9">https://medium.com/flutter-community/the-flutter-getx-ecosystem-dependency-injection-8e763d0ec6b9</a></p>
<p><a href="http://www.androidcoding.in/">http://www.androidcoding.in/</a></p>
<h1 id="首先我们不用任何状态管理pckage">首先我们不用任何状态管理pckage</h1>
<h2 id="maindart"><strong>main.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter_themes_demo/themes.dart&#39;</span>;

<span style="color:#66d9ef">void</span> main() {
  runApp(MyApp());
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyApp</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  MyApp({Key key}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);

  <span style="color:#960050;background-color:#1e0010">@</span>override
  _MyAppState createState() <span style="color:#f92672">=&gt;</span> _MyAppState();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_MyAppState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>MyApp<span style="color:#f92672">&gt;</span> {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  <span style="color:#66d9ef">void</span> initState() {
    <span style="color:#66d9ef">super</span>.initState();
    currentTheme.addListener(() {
      setState(() {});
    });
  }

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> MaterialApp(
      home: MyHomePage(title: <span style="color:#e6db74">&#39;Flutter Theme Demo&#39;</span>),
      title: <span style="color:#e6db74">&#39;Flutter Theme Demo&#39;</span>,
      theme: CustomTheme.lightTheme,
      darkTheme: CustomTheme.darkTheme,
      themeMode: currentTheme.currentTheme,
    );
  }
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyHomePage</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  MyHomePage({Key key, <span style="color:#66d9ef">this</span>.title}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);
  <span style="color:#66d9ef">final</span> <span style="color:#66d9ef">String</span> title;

  <span style="color:#960050;background-color:#1e0010">@</span>override
  _MyHomePageState createState() <span style="color:#f92672">=&gt;</span> _MyHomePageState();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_MyHomePageState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>MyHomePage<span style="color:#f92672">&gt;</span> {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">final</span> theme <span style="color:#f92672">=</span> Theme.of(context);

    <span style="color:#66d9ef">return</span> Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title,
          style: TextStyle(
            color: theme.accentColor,
          ),
        ),
        actions: [
          IconButton(
            icon: <span style="color:#66d9ef">const</span> Icon(Icons.brightness_4_rounded),
            onPressed: () {
              currentTheme.toggleTheme();
            },
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
            Text(
              <span style="color:#e6db74">&#39;Fluter Themes Demo&#39;</span>,
            ),
          ],
        ),
      ),
    );
  }
}
</code></pre></div><h2 id="themesdart"><strong>themes.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;

CustomTheme currentTheme <span style="color:#f92672">=</span> CustomTheme();

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CustomTheme</span> <span style="color:#66d9ef">with</span> ChangeNotifier {
  <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">bool</span> _isDarkTheme <span style="color:#f92672">=</span> <span style="color:#66d9ef">false</span>;
  ThemeMode <span style="color:#66d9ef">get</span> currentTheme <span style="color:#f92672">=&gt;</span> _isDarkTheme <span style="color:#f92672">?</span> ThemeMode.dark <span style="color:#f92672">:</span> ThemeMode.light;

  <span style="color:#66d9ef">void</span> toggleTheme() {
    _isDarkTheme <span style="color:#f92672">=</span> <span style="color:#f92672">!</span>_isDarkTheme;
    notifyListeners();
  }

  <span style="color:#66d9ef">static</span> ThemeData <span style="color:#66d9ef">get</span> lightTheme {
    <span style="color:#66d9ef">return</span> ThemeData(
      primaryColor: Colors.lightBlue,
      accentColor: Colors.white,
      backgroundColor: Colors.white,
      scaffoldBackgroundColor: Colors.white,
      textTheme: TextTheme(
        headline1: TextStyle(color: Colors.black),
        headline2: TextStyle(color: Colors.black),
        bodyText1: TextStyle(color: Colors.black),
        bodyText2: TextStyle(color: Colors.black),
      ),
    );
  }

  <span style="color:#66d9ef">static</span> ThemeData <span style="color:#66d9ef">get</span> darkTheme {
    <span style="color:#66d9ef">return</span> ThemeData(
      primaryColor: Colors.black,
      accentColor: Colors.red,
      backgroundColor: Colors.grey,
      scaffoldBackgroundColor: Colors.grey,
      textTheme: TextTheme(
        headline1: TextStyle(color: Colors.white),
        headline2: TextStyle(color: Colors.white),
        bodyText1: TextStyle(color: Colors.white),
        bodyText2: TextStyle(color: Colors.white),
      ),
    );
  }
}
</code></pre></div><h1 id="第二种方式-provider">第二种方式 provider</h1>
<p>先看我们的代码结构</p>
<p>
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210821204534589.png">
            <img class="mx-auto" alt="image-20210821204534589" src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210821204534589.png" />
        </a>
    </p>
<h2 id="maindart-1">main.dart</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/services.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider/provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:theme_example/page/home_page.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:theme_example/provider/theme_provider.dart&#39;</span>;

Future main() <span style="color:#66d9ef">async</span> {
  WidgetsFlutterBinding.ensureInitialized();
  <span style="color:#66d9ef">await</span> SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  runApp(MyApp());
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyApp</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">const</span> <span style="color:#66d9ef">String</span> title <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Light &amp; Dark Theme&#39;</span>;

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) <span style="color:#f92672">=&gt;</span> ChangeNotifierProvider(
        create: (context) <span style="color:#f92672">=&gt;</span> ThemeProvider(),
        builder: (context, _) {
          <span style="color:#66d9ef">final</span> themeProvider <span style="color:#f92672">=</span> Provider.of<span style="color:#f92672">&lt;</span>ThemeProvider<span style="color:#f92672">&gt;</span>(context);

          <span style="color:#66d9ef">return</span> MaterialApp(
            title: title,
            themeMode: themeProvider.themeMode,
            theme: MyThemes.lightTheme,
            darkTheme: MyThemes.darkTheme,
            home: HomePage(),
          );
        },
      );
}
</code></pre></div><h2 id="provider文件夹">provider文件夹</h2>
<p><strong>theme_provider.dart</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/scheduler.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ThemeProvider</span> <span style="color:#66d9ef">extends</span> ChangeNotifier {
  ThemeMode themeMode <span style="color:#f92672">=</span> ThemeMode.system;

  <span style="color:#66d9ef">bool</span> <span style="color:#66d9ef">get</span> isDarkMode {
    <span style="color:#66d9ef">if</span> (themeMode <span style="color:#f92672">==</span> ThemeMode.system) {
      <span style="color:#66d9ef">final</span> brightness <span style="color:#f92672">=</span> SchedulerBinding.instance.window.platformBrightness;
      <span style="color:#66d9ef">return</span> brightness <span style="color:#f92672">==</span> Brightness.dark;
    } <span style="color:#66d9ef">else</span> {
      <span style="color:#66d9ef">return</span> themeMode <span style="color:#f92672">==</span> ThemeMode.dark;
    }
  }

  <span style="color:#66d9ef">void</span> toggleTheme(<span style="color:#66d9ef">bool</span> isOn) {
    themeMode <span style="color:#f92672">=</span> isOn <span style="color:#f92672">?</span> ThemeMode.dark <span style="color:#f92672">:</span> ThemeMode.light;
    notifyListeners();
  }
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyThemes</span> {
  <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">final</span> darkTheme <span style="color:#f92672">=</span> ThemeData(
    scaffoldBackgroundColor: Colors.grey.shade900,
    primaryColor: Colors.black,
    colorScheme: ColorScheme.dark(),
    iconTheme: IconThemeData(color: Colors.purple.shade200, opacity: <span style="color:#ae81ff">0.8</span>),
  );

  <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">final</span> lightTheme <span style="color:#f92672">=</span> ThemeData(
    scaffoldBackgroundColor: Colors.white,
    primaryColor: Colors.white,
    colorScheme: ColorScheme.light(),
    iconTheme: IconThemeData(color: Colors.red, opacity: <span style="color:#ae81ff">0.8</span>),
  );
}
</code></pre></div><h2 id="page文件夹">page文件夹</h2>
<h3 id="home_pagedart"><strong>home_page.dart</strong></h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider/provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:theme_example/main.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:theme_example/page/profile_widget.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:theme_example/provider/theme_provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:theme_example/widget/change_theme_button_widget.dart&#39;</span>;

<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;navigationbar_widget.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">HomePage</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#75715e">// @override
</span><span style="color:#75715e"></span>  <span style="color:#75715e">// Widget build(BuildContext context) =&gt; Scaffold(
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//       appBar: AppBar(
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         iconTheme: Theme.of(context).iconTheme,
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         backgroundColor: Colors.transparent,
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         leading: Icon(Icons.menu),
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         title: Text(MyApp.title),
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         elevation: 0,
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         actions: [
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//           ChangeThemeButtonWidget(),
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//         ],
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//       ),
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//       body: ProfileWidget(),
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//       extendBody: true,
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//       bottomNavigationBar: NavigationBarWidget(),
</span><span style="color:#75715e"></span>  <span style="color:#75715e">//     );
</span><span style="color:#75715e"></span>
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">final</span> text <span style="color:#f92672">=</span> Provider.of<span style="color:#f92672">&lt;</span>ThemeProvider<span style="color:#f92672">&gt;</span>(context).themeMode <span style="color:#f92672">==</span> ThemeMode.dark
        <span style="color:#f92672">?</span> <span style="color:#e6db74">&#39;DarkTheme&#39;</span>
        <span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;LightTheme&#39;</span>;

    <span style="color:#66d9ef">return</span> Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.orange,
        title: Text(MyApp.title),
        actions: [
          ChangeThemeButtonWidget(),
        ],
      ),
      body: Center(
        child: Text(
          <span style="color:#e6db74">&#39;Hello </span><span style="color:#e6db74">$</span>text<span style="color:#e6db74">!&#39;</span>,
          style: TextStyle(
            fontSize: <span style="color:#ae81ff">32</span>,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}
</code></pre></div><h3 id="navigationbar_widgetdart"><strong>navigationbar_widget.dart</strong></h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">NavigationBarWidget</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) <span style="color:#f92672">=&gt;</span> Container(
        margin: <span style="color:#66d9ef">const</span> EdgeInsets.symmetric(vertical: <span style="color:#ae81ff">12</span>, horizontal: <span style="color:#ae81ff">16</span>),
        child: Container(
          height: <span style="color:#ae81ff">60</span>,
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(color: Colors.grey),
            ],
            color: Theme.of(context).primaryColor,
            borderRadius: BorderRadius.circular(<span style="color:#ae81ff">10</span>),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Icon(Icons.home_rounded),
              Icon(Icons.search),
              Icon(Icons.camera_alt),
              Icon(Icons.notifications),
              Icon(Icons.person)
            ],
          ),
        ),
      );
}
</code></pre></div><h3 id="profile_widgetdart"><strong>profile_widget.dart</strong></h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ProfileWidget</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#66d9ef">const</span> ProfileWidget({
    Key key,
  }) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) <span style="color:#f92672">=&gt;</span> ListView(
        physics: BouncingScrollPhysics(),
        children: [
          buildProfile(context),
          Divider(height: <span style="color:#ae81ff">32</span>),
          buildPhotos(context),
        ],
      );

  Widget buildPhotos(BuildContext context) <span style="color:#f92672">=&gt;</span> Container(
        padding: EdgeInsets.symmetric(horizontal: <span style="color:#ae81ff">12</span>),
        child: Column(
          children: [
            Align(
              alignment: Alignment.centerLeft,
              child: Text(
                <span style="color:#e6db74">&#39;PHOTOS&#39;</span>,
                style: TextStyle(
                  color: Theme.of(context).iconTheme.color,
                  fontSize: <span style="color:#ae81ff">16</span>,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            SizedBox(height: <span style="color:#ae81ff">12</span>),
            buildPhotoGrid(),
          ],
        ),
      );

  Widget buildPhotoGrid() <span style="color:#f92672">=&gt;</span> StaggeredGridView.countBuilder(
        primary: <span style="color:#66d9ef">false</span>,
        shrinkWrap: <span style="color:#66d9ef">true</span>,
        crossAxisCount: <span style="color:#ae81ff">4</span>,
        mainAxisSpacing: <span style="color:#ae81ff">8</span>,
        crossAxisSpacing: <span style="color:#ae81ff">8</span>,
        itemCount: <span style="color:#ae81ff">8</span>,
        itemBuilder: (BuildContext context, <span style="color:#66d9ef">int</span> index) {
          <span style="color:#66d9ef">final</span> urlImage <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://source.unsplash.com/random?sig=</span><span style="color:#e6db74">$</span>index<span style="color:#e6db74">&#39;</span>;

          <span style="color:#66d9ef">return</span> Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(<span style="color:#ae81ff">10</span>),
              image: DecorationImage(
                image: NetworkImage(urlImage),
                fit: BoxFit.cover,
              ),
            ),
          );
        },
        staggeredTileBuilder: (<span style="color:#66d9ef">int</span> index) <span style="color:#f92672">=&gt;</span>
            StaggeredTile.count(<span style="color:#ae81ff">2</span>, index.isEven <span style="color:#f92672">?</span> <span style="color:#ae81ff">2</span> <span style="color:#f92672">:</span> <span style="color:#ae81ff">1</span>),
      );

  Widget buildProfile(BuildContext context) <span style="color:#f92672">=&gt;</span> Column(
        children: [
          SizedBox(height: <span style="color:#ae81ff">20</span>),
          CircleAvatar(
            radius: <span style="color:#ae81ff">46</span>,
            backgroundColor: Theme.of(context).iconTheme.color,
            child: CircleAvatar(
              radius: <span style="color:#ae81ff">44</span>,
              backgroundColor: Theme.of(context).scaffoldBackgroundColor,
              child: CircleAvatar(
                child: Image.asset(<span style="color:#e6db74">&#39;assets/user1.png&#39;</span>),
                backgroundColor: Colors.pink,
                radius: <span style="color:#ae81ff">40</span>,
              ),
            ),
          ),
          SizedBox(height: <span style="color:#ae81ff">20</span>),
          Text(
            <span style="color:#e6db74">&#39;Jane Doe&#39;</span>,
            style: TextStyle(
              fontSize: <span style="color:#ae81ff">24</span>,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: <span style="color:#ae81ff">8</span>),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(Icons.location_on),
              <span style="color:#66d9ef">const</span> SizedBox(width: <span style="color:#ae81ff">4</span>),
              Text(<span style="color:#e6db74">&#39;New York, USA&#39;</span>),
            ],
          ),
          SizedBox(height: <span style="color:#ae81ff">16</span>),
          Text(<span style="color:#e6db74">&#39;Photography is the story I fail to put into words&#39;</span>),
          SizedBox(height: <span style="color:#ae81ff">16</span>),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              buildCounters(context, <span style="color:#e6db74">&#39;36&#39;</span>, <span style="color:#e6db74">&#39;Posts&#39;</span>),
              buildCounters(context, <span style="color:#e6db74">&#39;3.5K&#39;</span>, <span style="color:#e6db74">&#39;Following&#39;</span>),
              buildCounters(context, <span style="color:#e6db74">&#39;15K&#39;</span>, <span style="color:#e6db74">&#39;Followers&#39;</span>),
            ],
          ),
        ],
      );

  Widget buildCounters(context, <span style="color:#66d9ef">String</span> firstLine, <span style="color:#66d9ef">String</span> secondLine) <span style="color:#f92672">=&gt;</span> Column(
        children: [
          Text(
            firstLine,
            style: TextStyle(
              fontSize: <span style="color:#ae81ff">20</span>,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: <span style="color:#ae81ff">8</span>),
          Text(secondLine),
        ],
      );
}
</code></pre></div><h2 id="pubspecyaml"><strong>pubspec.yaml</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: <span style="color:#f92672">^</span><span style="color:#ae81ff">1.0</span>.<span style="color:#ae81ff">0</span>

  <span style="color:#960050;background-color:#1e0010">#</span> display photos <span style="color:#66d9ef">in</span> grid
  flutter_staggered_grid_view: <span style="color:#f92672">^</span><span style="color:#ae81ff">0.3</span>.<span style="color:#ae81ff">3</span>

  <span style="color:#960050;background-color:#1e0010">#</span> state management
  provider: <span style="color:#f92672">^</span><span style="color:#ae81ff">4.3</span>.<span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">3</span>

</code></pre></div><h1 id="第三种flutter-中应用主题创建自定义主题">第三种Flutter 中应用主题、创建自定义主题</h1>
<p><a href="https://www.youtube.com/watch?v=ju1fr9C4g4U">https://www.youtube.com/watch?v=ju1fr9C4g4U</a></p>
<p>
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210821205408798.png">
            <img class="mx-auto" alt="image-20210821205408798" src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210821205408798.png" />
        </a>
    </p>
<h2 id="maindart-2"><strong>main.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutterfetchtaxiapp/utils/theme.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;ui/screens/homescreen.dart&#39;</span>;

<span style="color:#66d9ef">void</span> main() <span style="color:#f92672">=&gt;</span> runApp(MyApp());

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyApp</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> MaterialApp(
      debugShowCheckedModeBanner: <span style="color:#66d9ef">false</span>,
      title: <span style="color:#e6db74">&#39;Flutter Taxi App&#39;</span>,
      <span style="color:#75715e">// theme: ThemeData(
</span><span style="color:#75715e"></span>      <span style="color:#75715e">//   primaryColor: Color(0xffdd6b3d),
</span><span style="color:#75715e"></span>      <span style="color:#75715e">// ),
</span><span style="color:#75715e"></span>      theme: basicTheme(),
      home: HomeScreen(),
    );
  }
}
</code></pre></div><h2 id="listviewdetailsscreendart"><strong>Listviewdetailsscreen.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutterfetchtaxiapp/model/carservices.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:font_awesome_flutter/font_awesome_flutter.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutterfetchtaxiapp/utils/widgets/starWidget.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutterfetchtaxiapp/utils/widgets/profilewidget.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ListViewDetailsScreen</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  ListViewDetailsScreen({Key key, <span style="color:#66d9ef">this</span>.carServices}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);
  <span style="color:#66d9ef">final</span> CarServices carServices;

  _ListViewDetailsScreenState createState() <span style="color:#f92672">=&gt;</span>
      _ListViewDetailsScreenState(carServices: <span style="color:#66d9ef">this</span>.carServices);
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_ListViewDetailsScreenState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>ListViewDetailsScreen<span style="color:#f92672">&gt;</span> {
  CarServices carServices;
  Color _colorVal;

  _ListViewDetailsScreenState({<span style="color:#66d9ef">this</span>.carServices});

  <span style="color:#66d9ef">void</span> _favServices() {
    setState(() {
      _colorVal <span style="color:#f92672">=</span> Colors.red;
    });
  }

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> Scaffold(
      appBar: AppBar(
        actions: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
          Row(
            children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
              IconButton(
                icon: Icon(
                  FontAwesomeIcons.solidHeart,
                  size: <span style="color:#ae81ff">30.0</span>,
                  color: _colorVal,
                ),
                onPressed: () {
                 _favServices();
                },
              ),
              SizedBox(
                width: <span style="color:#ae81ff">25.0</span>,
              ),
              Icon(
                FontAwesomeIcons.pen,
                size: <span style="color:#ae81ff">25.0</span>,
                color: Theme.of(context).buttonColor,
              ),
              SizedBox(
                width: <span style="color:#ae81ff">5.0</span>,
              ),
            ],
          )
        ],
        elevation: <span style="color:#ae81ff">0</span>,
      ),
      body: Column(
        children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
          Container(
              color: Theme.of(context).primaryColor,
              width: MediaQuery.of(context).size.width,
              height: <span style="color:#ae81ff">300.0</span>,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
                  Icon(
                    FontAwesomeIcons.carSide,
                    size: <span style="color:#ae81ff">120.0</span>,
                    color: Theme.of(context).buttonColor,
                  ),
                  SizedBox(
                    height: <span style="color:#ae81ff">60.0</span>,
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: <span style="color:#ae81ff">16.0</span>),
                    child: Column(
                      children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            carServices.carService,
                            style: Theme.of(context).textTheme.display1,
                          ),
                        ),
                        SizedBox(height: <span style="color:#ae81ff">5.0</span>),
                        StarWidget(
                          numberOfStars:
                              <span style="color:#66d9ef">int</span>.parse(carServices.carRate.toString()),
                        ),
                      ],
                    ),
                  ),
                ],
              )),
          Card(
            elevation: <span style="color:#ae81ff">3.0</span>,
            child: Padding(
              padding: EdgeInsets.only(top: <span style="color:#ae81ff">16.0</span>, bottom: <span style="color:#ae81ff">16.0</span>),
              child: ListTile(
                title: Row(
                  children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
                    Expanded(
                      child: Column(
                        children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              <span style="color:#e6db74">&#39;+123 456 7890&#39;</span>,
                              style: Theme.of(context).textTheme.display2,
                            ),
                          ),
                          SizedBox(height: <span style="color:#ae81ff">5.0</span>),
                          Align(
                              alignment: Alignment.centerLeft,
                              child: Text(<span style="color:#e6db74">&#39;Office&#39;</span>)),
                        ],
                      ),
                    ),
                    IconButton(
                      icon: Icon(
                        FontAwesomeIcons.phone,
                        color: Theme.of(context).textTheme.title.color,
                      ),
                      onPressed: () {
                        <span style="color:#75715e">//
</span><span style="color:#75715e"></span>                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
          ProfileWidget(),
        ],
      ),
    );
  }
}
</code></pre></div><h2 id="homescreendart"><strong>homescreen.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:font_awesome_flutter/font_awesome_flutter.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutterfetchtaxiapp/utils/widgets/serviceswidget.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutterfetchtaxiapp/utils/widgets/favouritewidget.dart&#39;</span>;


<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">HomeScreen</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  HomeScreen({Key key}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);

  _HomeScreenState createState() <span style="color:#f92672">=&gt;</span> _HomeScreenState();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_HomeScreenState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>HomeScreen<span style="color:#f92672">&gt;</span> {

  <span style="color:#66d9ef">int</span> _currentIndex <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
 <span style="color:#66d9ef">final</span> List<span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span> _children <span style="color:#f92672">=</span> [
   ServicesWidget(),
   FavouriteWidget()
 ];

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#75715e">// Build the content depending on the state:
</span><span style="color:#75715e"></span>    <span style="color:#66d9ef">return</span> Scaffold(
     appBar: AppBar(
       leading: Icon(FontAwesomeIcons.bars,
        color: Theme.of(context).iconTheme.color,
       ),
       title: Text(<span style="color:#e6db74">&#39;Fetch Taxi&#39;</span>),
     ),
     body: _children[_currentIndex], <span style="color:#75715e">// new
</span><span style="color:#75715e"></span>     bottomNavigationBar: BottomNavigationBar(
       onTap: onTabTapped, <span style="color:#75715e">// new
</span><span style="color:#75715e"></span>       currentIndex: _currentIndex, 
       items: [
         BottomNavigationBarItem(
           icon: <span style="color:#66d9ef">new</span> Icon(FontAwesomeIcons.car),
           title: <span style="color:#66d9ef">new</span> Text(<span style="color:#e6db74">&#39;Services&#39;</span>),
         ),
         BottomNavigationBarItem(
           icon: <span style="color:#66d9ef">new</span> Icon(FontAwesomeIcons.solidHeart),
           title: <span style="color:#66d9ef">new</span> Text(<span style="color:#e6db74">&#39;Favourite&#39;</span>),
         ),
       ],
     ),
   );
  }
  <span style="color:#66d9ef">void</span> onTabTapped(<span style="color:#66d9ef">int</span> index) {
   setState(() {
     _currentIndex <span style="color:#f92672">=</span> index;
   });
 }
}
</code></pre></div><h2 id="carservicesdart"><strong>carservices.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CarServices</span>{

  <span style="color:#66d9ef">final</span> <span style="color:#66d9ef">int</span> carId;
   <span style="color:#66d9ef">String</span> carService;
   <span style="color:#66d9ef">int</span> carRate;

  CarServices({<span style="color:#66d9ef">this</span>.carId,<span style="color:#66d9ef">this</span>.carService,<span style="color:#66d9ef">this</span>.carRate});
  <span style="color:#66d9ef">factory</span> CarServices.fromJson(Map<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">String</span>, <span style="color:#66d9ef">dynamic</span><span style="color:#f92672">&gt;</span> json) {
    <span style="color:#66d9ef">return</span> CarServices(
      carId: json[<span style="color:#e6db74">&#39;car_id&#39;</span>] <span style="color:#f92672">as</span> <span style="color:#66d9ef">int</span>,
      carService: json[<span style="color:#e6db74">&#39;car_service&#39;</span>] <span style="color:#f92672">as</span> <span style="color:#66d9ef">String</span>,
      carRate: json[<span style="color:#e6db74">&#39;car_rate&#39;</span>] <span style="color:#f92672">as</span> <span style="color:#66d9ef">int</span>,
    );
  }
}
</code></pre></div><h1 id="第四种方式package--stacked_themes">第四种方式package  stacked_themes</h1>
<p>本教程将介绍如何在堆叠式架构中实现多个主题。它是 Theme Manager Snippet 的一个实现，它使用纯提供程序作为主题管理器。我们正在使用该教程中显示的完全相同的原则。如果您只想使用 provider 构建主题管理器，那么您可以查看它。要继续下载本指南中将使用<a href="https://firebasestorage.googleapis.com/v0/b/filledstacks.appspot.com/o/tutorials%2F056%2F056-starting.zip?alt=media&amp;token=7b4ea964-dc93-43aa-95c2-f0f6f19f1dc0">的起始代码，请点击此处</a>。</p>
<p><a href="https://www.filledstacks.com/post/multiple-themes-in-flutter-dark-and-light-theme-flutter-stacked/">https://www.filledstacks.com/post/multiple-themes-in-flutter-dark-and-light-theme-flutter-stacked/</a></p>
<h2 id="设置和堆叠主题">设置和堆叠主题</h2>
<p>我们首先将stacked_themes 包添加到pubspec.yaml 文件中</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml"><span style="color:#f92672">dependencies</span>: <span style="color:#ae81ff">...</span>
  <span style="color:#f92672">stacked_themes</span>:
</code></pre></div><p>这个包提供了一些基本的类，以便在使用 stack 构建应用程序时使主题管理更容易。stacked_themes 为您提供了交换提供给您的应用程序的 ThemeData 的基本功能，可以使用<code>Theme.of(context)</code>. 除此之外，它还为您提供了一个帮助功能，您可以在其中更改状态栏颜色。让我们来看看代码。</p>
<h2 id="用法">用法</h2>
<p>该软件包使用 a<code>ThemeManager</code>来管理所有主题功能。首先，我们必须<code>ThemeManager</code>在应用程序启动之前调用 的初始化函数。更改您的主函数以返回 a<code>Future</code>并使其异步并等待初始化静态函数调用<code>ThemeManager</code>。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">Future main() <span style="color:#66d9ef">async</span> {
  <span style="color:#66d9ef">await</span> ThemeManager.initialise();
  runApp(MyApp());
}
</code></pre></div><h3 id="主题生成器">主题生成器</h3>
<p>我们通过包装我们的开始<code>MaterialApp</code>或任何其他<code>App</code>与我们的<code>ThemeBuilder</code>。builder 函数返回一个上下文、regularTheme、darkTheme 和一个 themeMode。我们将此提供给<code>MaterialApp</code>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyApp</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> ThemeBuilder(
      builder: (context, regularTheme, darkTheme, themeMode) <span style="color:#f92672">=&gt;</span> MaterialApp(
        title: <span style="color:#e6db74">&#39;Flutter Demo&#39;</span>,
        theme: regularTheme,
        darkTheme: darkTheme,
        themeMode: themeMode,
        ...
      ),
    );
  }
}
</code></pre></div><p>这将重建您的应用程序并通过上下文传递一个新主题供您在应用程序中使用。</p>
<h3 id="提供多个主题">提供多个主题</h3>
<p>该<code>ThemeBuilder</code>有一个属性叫做主题，在那里你可以提供的列表<code>ThemeData</code>。这通常只有 2 或 3 个，浅色、深色、中性色，但我们将其列在一个列表中，这样您就可以拥有任意数量的。为了保持整洁，我们将从在不同文件中创建的函数返回主题列表。创建一个名为<code>theme_setup.dart</code>. 您可以将其放置在 UI 文件夹的根位置或将其保留在 lib 的根目录中。我还没有决定哪里最好，所以这取决于你</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">List<span style="color:#f92672">&lt;</span>ThemeData<span style="color:#f92672">&gt;</span> getThemes() {
  <span style="color:#66d9ef">return</span> [
    ThemeData(backgroundColor: Colors.blue, accentColor: Colors.yellow),
    ThemeData(backgroundColor: Colors.white, accentColor: Colors.green),
    ThemeData(backgroundColor: Colors.purple, accentColor: Colors.green),
    ThemeData(backgroundColor: Colors.black, accentColor: Colors.red),
    ThemeData(backgroundColor: Colors.red, accentColor: Colors.blue),
  ];
}
</code></pre></div><p>这是一个非常简单的功能，但考虑到您将在主题中使用多少主题属性，它可能会开始看起来很忙。这就是为什么我建议您将其保存在单独的文件中。返回您的 main.dart 文件并为 .dart<code>getThemes()</code>的<code>themes</code>属性提供值<code>ThemeBuilder</code>。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">Widget build(BuildContext context) {
  <span style="color:#66d9ef">return</span> ThemeBuilder(
    themes: getThemes(),
    builder: (context, regularTheme, darkTheme, themeMode) <span style="color:#f92672">=&gt;</span> MaterialApp(
      title: <span style="color:#e6db74">&#39;Flutter Demo&#39;</span>,
      theme: regularTheme,
      darkTheme: darkTheme,
      themeMode: themeMode,
      ...
    ),
  );
}
</code></pre></div><p>我们在主题之间切换的方式是设置我们要使用的主题的索引。它将从列表中的第一个主题开始。要将主题更新为任何其他主题，您只需在 UI 中调用<code>getThemeManager(context)</code>获取<code>ThemeManager</code>然后调用<code>selectThemeAtIndex(1)</code>它即可。这是在提供的主题列表中选择第二个主题的代码。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">getThemeManager(context)
  .selectThemeAtIndex(<span style="color:#ae81ff">1</span>);
</code></pre></div><h3 id="light-和-dark-主题">Light 和 Dark 主题</h3>
<p>如果您只想使用 Light 和 Dark 主题，那么您可以将其提供给<code>ThemeBuilder</code>而不是提供多个主题。在提供 Light 和 Dark 模式时，您还可以提供 defaultThemeMode，这是<code>ThemeMode.system</code>默认的。当您离开时，您是<code>system</code>在告诉<code>ThemeManager</code>您希望系统确定您使用的是亮模式还是暗模式。这就是您提供深色和浅色主题的方式。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">Widget build(BuildContext context) {
  <span style="color:#66d9ef">return</span> ThemeBuilder(
    defaultThemeMode: ThemeMode.light,
    darkTheme: ThemeData(
      brightness: Brightness.dark,
      backgroundColor: Colors.blue[<span style="color:#ae81ff">700</span>],
      accentColor: Colors.yellow[<span style="color:#ae81ff">700</span>],
    ),
    lightTheme: ThemeData(
      brightness: Brightness.light,
      backgroundColor: Colors.blue[<span style="color:#ae81ff">300</span>],
      accentColor: Colors.yellow[<span style="color:#ae81ff">300</span>],
    ),
    builder: (context, regularTheme, darkTheme, themeMode) <span style="color:#f92672">=&gt;</span> MaterialApp(
      title: <span style="color:#e6db74">&#39;Flutter Demo&#39;</span>,
      theme: regularTheme,
      darkTheme: darkTheme,
      themeMode: themeMode,
      ...
    ),
  );
}
</code></pre></div><p>当<code>defaultThemeMode</code>不是系统时，如上例所示，您可以使用该<code>toggleDarkLightTheme</code>功能在明暗主题之间切换。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">getThemeManager(context)
  .toggleDarkLightTheme();
</code></pre></div><h3 id="更改状态栏颜色">更改状态栏颜色</h3>
<p>有时，当您有特定主题时，您也希望状态栏颜色也发生变化。大多数应用程序将此透明设置为使用背景颜色。如果您的应用程序要求您更改状态栏颜色，那么也有相应的功能。该<code>ThemeBuilder</code>有一个属性<code>statusBarColorBuilder</code>它是希望你返回的功能<code>Color</code>。它为您提供了要选择的主题，您可以使用它来返回要为状态栏显示的颜色。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">Widget build(BuildContext context) {
  <span style="color:#66d9ef">return</span> ThemeBuilder(
    statusBarColorBuilder: (theme) <span style="color:#f92672">=&gt;</span> theme.accentColor,
    lightTheme: ThemeData(...),
    darkTheme: ThemeData(...),
    ...
  );
}
</code></pre></div><p>在上面的例子中，我们返回主题的重音颜色作为状态栏颜色。此功能将自动检查状态文本是否可见，并将根据背景值强度(与白色/黑色的接近程度)将文本从白色更改为黑色。</p>
<h3 id="主题持久化">主题持久化]</h3>
<p>如果用户在应用程序的生命周期内更改了任何主题，则在应用程序再次启动时将自动应用上次选择的主题。此功能是通过使用<a href="https://pub.dev/packages/shared_preferences">shared_preferences</a>包创建的。</p>
<h2 id="使用主题">使用主题</h2>
<p>现在您已经设置了所有主题功能，让我们看看如何在您的 UI 中使用它。首先要知道的是，您可以以正常方式访问主题数据。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">var</span> theme <span style="color:#f92672">=</span> Theme.of(context);
</code></pre></div><p>这意味着您的 Flutter 代码的其余部分仍然保持不变。</p>
<h3 id="业务逻辑中的访问">业务逻辑中的访问</h3>
<p>如你所知。我也坚持的规则之一是，不应在任何业务逻辑中直接使用 UI 相关代码或类。出于这个原因，还有一个<code>ThemeService</code>允许您调用在主题之间更改所需的相同功能。此服务应用作您过去使用过的任何其他stacked_services。首先我们用定位器注册它。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">locator.registerSingleton(ThemeService.getInstance());

<span style="color:#75715e">// or add it to your third_party_services_module if you’re using injectable
</span></code></pre></div><p>然后<code>ViewModel</code>通过从<code>locator</code>. 就是这样。这就是所需的所有功能。如果您想更多地讨论这个问题，请查看 slack 并在那里提出您的问题。如果您想请求一些附加功能或更改，请不要犹豫，</p>
<h1 id="第五种方式-provider">第五种方式 provider</h1>
<p>
        <a data-fancybox="gallery" href="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210821210815695.png">
            <img class="mx-auto" alt="image-20210821210815695" src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210821210815695.png" />
        </a>
    </p>
<h2 id="maindart-3">main.dart</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:hive/hive.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:light_dark_theme_flutter/models_providers/theme_provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:light_dark_theme_flutter/pages/home_page.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:path_provider/path_provider.dart&#39;</span> <span style="color:#66d9ef">as</span> pathProvider;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider/provider.dart&#39;</span>;

<span style="color:#66d9ef">void</span> main() <span style="color:#66d9ef">async</span> {
  WidgetsFlutterBinding.ensureInitialized();
  <span style="color:#66d9ef">final</span> appDocumentDirectory <span style="color:#f92672">=</span>
      <span style="color:#66d9ef">await</span> pathProvider.getApplicationDocumentsDirectory();

  Hive.init(appDocumentDirectory.path);

  <span style="color:#66d9ef">final</span> settings <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> Hive.openBox(<span style="color:#e6db74">&#39;settings&#39;</span>);
  <span style="color:#66d9ef">bool</span> isLightTheme <span style="color:#f92672">=</span> settings.<span style="color:#66d9ef">get</span>(<span style="color:#e6db74">&#39;isLightTheme&#39;</span>) <span style="color:#f92672">??</span> <span style="color:#66d9ef">false</span>;

  print(isLightTheme);

  runApp(ChangeNotifierProvider(
    create: (_) <span style="color:#f92672">=&gt;</span> ThemeProvider(isLightTheme: isLightTheme),
    child: AppStart(),
  ));
}

<span style="color:#75715e">// to ensure we have the themeProvider before the app starts lets make a few more changes.
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">AppStart</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#66d9ef">const</span> AppStart({Key key}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    ThemeProvider themeProvider <span style="color:#f92672">=</span> Provider.of<span style="color:#f92672">&lt;</span>ThemeProvider<span style="color:#f92672">&gt;</span>(context);
    <span style="color:#66d9ef">return</span> MyApp(
      themeProvider: themeProvider,
    );
  }
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyApp</span> <span style="color:#66d9ef">extends</span> StatefulWidget <span style="color:#66d9ef">with</span> WidgetsBindingObserver {
  <span style="color:#66d9ef">final</span> ThemeProvider themeProvider;

  <span style="color:#66d9ef">const</span> MyApp({Key key, <span style="color:#960050;background-color:#1e0010">@</span>required <span style="color:#66d9ef">this</span>.themeProvider}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);
  <span style="color:#960050;background-color:#1e0010">@</span>override
  _MyAppState createState() <span style="color:#f92672">=&gt;</span> _MyAppState();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_MyAppState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>MyApp<span style="color:#f92672">&gt;</span> {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> MaterialApp(
      debugShowCheckedModeBanner: <span style="color:#66d9ef">false</span>,
      title: <span style="color:#e6db74">&#39;Light Dark Theme&#39;</span>,
      theme: widget.themeProvider.themeData(),
      home: HomePage(),
    );
  }
}
</code></pre></div><h2 id="home_pagedart-1"><strong>home_page.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:light_dark_theme_flutter/components/z_animated_toggle.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:light_dark_theme_flutter/models_providers/theme_provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider/provider.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">HomePage</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  HomePage({Key key}) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);

  <span style="color:#960050;background-color:#1e0010">@</span>override
  _HomePageState createState() <span style="color:#f92672">=&gt;</span> _HomePageState();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_HomePageState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>HomePage<span style="color:#f92672">&gt;</span>
    <span style="color:#66d9ef">with</span> SingleTickerProviderStateMixin {
  AnimationController _animationController;
  GlobalKey<span style="color:#f92672">&lt;</span>ScaffoldState<span style="color:#f92672">&gt;</span> _scaffoldKey <span style="color:#f92672">=</span> GlobalKey<span style="color:#f92672">&lt;</span>ScaffoldState<span style="color:#f92672">&gt;</span>();

  <span style="color:#960050;background-color:#1e0010">@</span>override
  <span style="color:#66d9ef">void</span> initState() {
    _animationController <span style="color:#f92672">=</span>
        AnimationController(vsync: <span style="color:#66d9ef">this</span>, duration: Duration(milliseconds: <span style="color:#ae81ff">800</span>));
    <span style="color:#66d9ef">super</span>.initState();
  }

  <span style="color:#75715e">// function to toggle circle animation
</span><span style="color:#75715e"></span>  changeThemeMode(<span style="color:#66d9ef">bool</span> theme) {
    <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span>theme) {
      _animationController.forward(from: <span style="color:#ae81ff">0.0</span>);
    } <span style="color:#66d9ef">else</span> {
      _animationController.reverse(from: <span style="color:#ae81ff">1.0</span>);
    }
  }

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">double</span> height <span style="color:#f92672">=</span> MediaQuery.of(context).size.height;
    <span style="color:#66d9ef">double</span> width <span style="color:#f92672">=</span> MediaQuery.of(context).size.width;
    <span style="color:#75715e">// Now we have access to the theme properties
</span><span style="color:#75715e"></span>    <span style="color:#66d9ef">final</span> themeProvider <span style="color:#f92672">=</span> Provider.of<span style="color:#f92672">&lt;</span>ThemeProvider<span style="color:#f92672">&gt;</span>(context);
    <span style="color:#66d9ef">return</span> Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        child: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(top: height <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.1</span>),
          child: Column(
            children: [
              Stack(
                children: [
                  Container(
                    width: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.35</span>,
                    height: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.35</span>,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      gradient: LinearGradient(
                          colors: themeProvider.themeMode().gradient,
                          begin: Alignment.bottomLeft,
                          end: Alignment.topRight),
                    ),
                  ),
                  Transform.translate(
                    offset: Offset(<span style="color:#ae81ff">40</span>, <span style="color:#ae81ff">0</span>),
                    child: ScaleTransition(
                      scale: _animationController.drive(
                        Tween<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">double</span><span style="color:#f92672">&gt;</span>(begin: <span style="color:#ae81ff">0.0</span>, end: <span style="color:#ae81ff">1.0</span>).chain(
                          CurveTween(curve: Curves.decelerate),
                        ),
                      ),
                      alignment: Alignment.topRight,
                      child: Container(
                        width: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">26</span>,
                        height: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">26</span>,
                        decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color: themeProvider.isLightTheme
                                <span style="color:#f92672">?</span> Colors.white
                                <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFF26242e</span>)),
                      ),
                    ),
                  )
                ],
              ),
              SizedBox(height: height <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.1</span>),
              Text(
                <span style="color:#e6db74">&#39;Choose a style&#39;</span>,
                style: TextStyle(
                    fontSize: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">06</span>, fontWeight: FontWeight.bold),
              ),
              SizedBox(height: height <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.03</span>),
              Container(
                width: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">6</span>,
                child: Text(
                  <span style="color:#e6db74">&#39;Pop or subtle. Day or night. Customize your interface&#39;</span>,
                  textAlign: TextAlign.center,
                ),
              ),
              SizedBox(height: height <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.1</span>),
              ZAnimatedToggle(
                values: [<span style="color:#e6db74">&#39;Light&#39;</span>, <span style="color:#e6db74">&#39;Dark&#39;</span>],
                onToggleCallback: (v) <span style="color:#66d9ef">async</span> {
                  <span style="color:#66d9ef">await</span> themeProvider.toggleThemeData();
                  setState(() {});
                  changeThemeMode(themeProvider.isLightTheme);
                },
              ),
              SizedBox(
                height: height <span style="color:#f92672">*</span> .<span style="color:#ae81ff">05</span>,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
                  buildDot(
                    width: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.022</span>,
                    height: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.022</span>,
                    color: <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFFd9d9d9</span>),
                  ),
                  buildDot(
                    width: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.055</span>,
                    height: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.022</span>,
                    color: themeProvider.isLightTheme
                        <span style="color:#f92672">?</span> Color(<span style="color:#ae81ff">0xFF26242e</span>)
                        <span style="color:#f92672">:</span> Colors.white,
                  ),
                  buildDot(
                    width: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.022</span>,
                    height: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.022</span>,
                    color: <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFFd9d9d9</span>),
                  ),
                ],
              ),
              <span style="color:#75715e">// skip &amp; next
</span><span style="color:#75715e"></span>              Expanded(
                child: Container(
                  margin: EdgeInsets.symmetric(
                      vertical: height <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.02</span>, horizontal: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.04</span>),
                  alignment: Alignment.bottomCenter,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
                      Padding(
                        padding:
                            EdgeInsets.symmetric(horizontal: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.025</span>),
                        child: Text(
                          <span style="color:#e6db74">&#39;Skip&#39;</span>,
                          style: TextStyle(
                            fontSize: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.045</span>,
                            color: <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFF7c7b7e</span>),
                            fontFamily: <span style="color:#e6db74">&#39;Rubik&#39;</span>,
                          ),
                        ),
                      ),
                      RaisedButton(
                        onPressed: () {
                          _scaffoldKey.currentState.showSnackBar(
                            SnackBar(
                              content: Text(
                                <span style="color:#e6db74">&#39;Loved it? Give a star on Github&#39;</span>,
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  fontFamily: <span style="color:#e6db74">&#39;Rubik&#39;</span>,
                                  fontSize: width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.045</span>,
                                ),
                              ),
                            ),
                          );
                        },
                        shape: CircleBorder(),
                        color: themeProvider.isLightTheme
                            <span style="color:#f92672">?</span> <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFFFFFFFF</span>)
                            <span style="color:#f92672">:</span> <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFF35303f</span>),
                        child: Padding(
                          padding: EdgeInsets.all(width <span style="color:#f92672">*</span> <span style="color:#ae81ff">0.05</span>),
                          child: Icon(
                            Icons.arrow_forward,
                            color: themeProvider.isLightTheme
                                <span style="color:#f92672">?</span> <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFF000000</span>)
                                <span style="color:#f92672">:</span> <span style="color:#66d9ef">const</span> Color(<span style="color:#ae81ff">0xFFFFFFFF</span>),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

<span style="color:#75715e">// for drawing the dots
</span><span style="color:#75715e"></span>  Container buildDot({<span style="color:#66d9ef">double</span> width, <span style="color:#66d9ef">double</span> height, Color color}) {
    <span style="color:#66d9ef">return</span> Container(
      margin: EdgeInsets.symmetric(horizontal: <span style="color:#ae81ff">4</span>),
      width: width,
      height: height,
      decoration: ShapeDecoration(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(<span style="color:#ae81ff">20</span>),
        ),
        color: color,
      ),
    );
  }
}

<span style="color:#75715e">// #time for finishing touches! I
</span></code></pre></div><h2 id="theme_providerdart"><strong>theme_provider.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#75715e">// we use provider to manage the app state
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/services.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:hive/hive.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ThemeProvider</span> <span style="color:#66d9ef">with</span> ChangeNotifier {
  <span style="color:#66d9ef">bool</span> isLightTheme;

  ThemeProvider({<span style="color:#66d9ef">this</span>.isLightTheme});

  <span style="color:#75715e">// the code below is to manage the status bar color when the theme changes
</span><span style="color:#75715e"></span>  getCurrentStatusNavigationBarColor() {
    <span style="color:#66d9ef">if</span> (isLightTheme) {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.dark,
        systemNavigationBarColor: Color(<span style="color:#ae81ff">0xFFFFFFFF</span>),
        systemNavigationBarIconBrightness: Brightness.dark,
      ));
    } <span style="color:#66d9ef">else</span> {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.dark,
        statusBarIconBrightness: Brightness.light,
        systemNavigationBarColor: Color(<span style="color:#ae81ff">0xFF26242e</span>),
        systemNavigationBarIconBrightness: Brightness.light,
      ));
    }
  }

  <span style="color:#75715e">// use to toggle the theme
</span><span style="color:#75715e"></span>  toggleThemeData() <span style="color:#66d9ef">async</span> {
    <span style="color:#66d9ef">final</span> settings <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> Hive.openBox(<span style="color:#e6db74">&#39;settings&#39;</span>);
    settings.put(<span style="color:#e6db74">&#39;isLightTheme&#39;</span>, <span style="color:#f92672">!</span>isLightTheme);
    isLightTheme <span style="color:#f92672">=</span> <span style="color:#f92672">!</span>isLightTheme;
    getCurrentStatusNavigationBarColor();
    notifyListeners();
  }

  <span style="color:#75715e">// Global theme data we are always check if the light theme is enabled #isLightTheme
</span><span style="color:#75715e"></span>  ThemeData themeData() {
    <span style="color:#66d9ef">return</span> ThemeData(
      visualDensity: VisualDensity.adaptivePlatformDensity,
      primarySwatch: isLightTheme <span style="color:#f92672">?</span> Colors.grey <span style="color:#f92672">:</span> Colors.grey,
      primaryColor: isLightTheme <span style="color:#f92672">?</span> Colors.white <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFF1E1F28</span>),
      brightness: isLightTheme <span style="color:#f92672">?</span> Brightness.light <span style="color:#f92672">:</span> Brightness.dark,
      backgroundColor: isLightTheme <span style="color:#f92672">?</span> Color(<span style="color:#ae81ff">0xFFFFFFFF</span>) <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFF26242e</span>),
      scaffoldBackgroundColor:
          isLightTheme <span style="color:#f92672">?</span> Color(<span style="color:#ae81ff">0xFFFFFFFF</span>) <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFF26242e</span>),
    );
  }

  <span style="color:#75715e">// Theme mode to display unique properties not cover in theme data
</span><span style="color:#75715e"></span>  ThemeColor themeMode() {
    <span style="color:#66d9ef">return</span> ThemeColor(
      gradient: [
        <span style="color:#66d9ef">if</span> (isLightTheme) ...[Color(<span style="color:#ae81ff">0xDDFF0080</span>), Color(<span style="color:#ae81ff">0xDDFF8C00</span>)],
        <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span>isLightTheme) ...[Color(<span style="color:#ae81ff">0xFF8983F7</span>), Color(<span style="color:#ae81ff">0xFFA3DAFB</span>)]
      ],
      textColor: isLightTheme <span style="color:#f92672">?</span> Color(<span style="color:#ae81ff">0xFF000000</span>) <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFFFFFFFF</span>),
      toggleButtonColor: isLightTheme <span style="color:#f92672">?</span> Color(<span style="color:#ae81ff">0xFFFFFFFF</span>) <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFf34323d</span>),
      toggleBackgroundColor:
          isLightTheme <span style="color:#f92672">?</span> Color(<span style="color:#ae81ff">0xFFe7e7e8</span>) <span style="color:#f92672">:</span> Color(<span style="color:#ae81ff">0xFF222029</span>),
      shadow: [
        <span style="color:#66d9ef">if</span> (isLightTheme)
          BoxShadow(
              color: Color(<span style="color:#ae81ff">0xFFd8d7da</span>),
              spreadRadius: <span style="color:#ae81ff">5</span>,
              blurRadius: <span style="color:#ae81ff">10</span>,
              offset: Offset(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">5</span>)),
        <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span>isLightTheme)
          BoxShadow(
              color: Color(<span style="color:#ae81ff">0x66000000</span>),
              spreadRadius: <span style="color:#ae81ff">5</span>,
              blurRadius: <span style="color:#ae81ff">10</span>,
              offset: Offset(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">5</span>))
      ],
    );
  }
}

<span style="color:#75715e">// A class to manage specify colors and styles in the app not supported by theme data
</span><span style="color:#75715e"></span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ThemeColor</span> {
  List<span style="color:#f92672">&lt;</span>Color<span style="color:#f92672">&gt;</span> gradient;
  Color backgroundColor;
  Color toggleButtonColor;
  Color toggleBackgroundColor;
  Color textColor;
  List<span style="color:#f92672">&lt;</span>BoxShadow<span style="color:#f92672">&gt;</span> shadow;

  ThemeColor({
    <span style="color:#66d9ef">this</span>.gradient,
    <span style="color:#66d9ef">this</span>.backgroundColor,
    <span style="color:#66d9ef">this</span>.toggleBackgroundColor,
    <span style="color:#66d9ef">this</span>.toggleButtonColor,
    <span style="color:#66d9ef">this</span>.textColor,
    <span style="color:#66d9ef">this</span>.shadow,
  });
}
</code></pre></div><h2 id="z_animated_toggledart"><strong>z_animated_toggle.dart</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:light_dark_theme_flutter/models_providers/theme_provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider/provider.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ZAnimatedToggle</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  <span style="color:#66d9ef">final</span> List<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">String</span><span style="color:#f92672">&gt;</span> values;
  <span style="color:#66d9ef">final</span> ValueChanged onToggleCallback;
  ZAnimatedToggle({
    Key key,
    <span style="color:#960050;background-color:#1e0010">@</span>required <span style="color:#66d9ef">this</span>.values,
    <span style="color:#960050;background-color:#1e0010">@</span>required <span style="color:#66d9ef">this</span>.onToggleCallback,
  }) <span style="color:#f92672">:</span> <span style="color:#66d9ef">super</span>(key: key);

  <span style="color:#960050;background-color:#1e0010">@</span>override
  _ZAnimatedToggleState createState() <span style="color:#f92672">=&gt;</span> _ZAnimatedToggleState();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_ZAnimatedToggleState</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>ZAnimatedToggle<span style="color:#f92672">&gt;</span> {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">double</span> width <span style="color:#f92672">=</span> MediaQuery.of(context).size.width;
    <span style="color:#66d9ef">final</span> themeProvider <span style="color:#f92672">=</span> Provider.of<span style="color:#f92672">&lt;</span>ThemeProvider<span style="color:#f92672">&gt;</span>(context);
    <span style="color:#66d9ef">return</span> Container(
      width: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">7</span>,
      height: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">13</span>,
      child: Stack(
        children: [
          GestureDetector(
            onTap: () {
              widget.onToggleCallback(<span style="color:#ae81ff">1</span>);
            },
            child: Container(
              width: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">7</span>,
              height: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">13</span>,
              decoration: ShapeDecoration(
                  color: themeProvider.themeMode().toggleBackgroundColor,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">1</span>))),
              child: Row(
                children: List.generate(
                  widget.values.length,
                  (index) <span style="color:#f92672">=&gt;</span> Padding(
                    padding: EdgeInsets.symmetric(horizontal: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">1</span>),
                    child: Text(
                      widget.values[index],
                      style: TextStyle(
                          fontSize: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">05</span>,
                          fontWeight: FontWeight.bold,
                          color: Color(<span style="color:#ae81ff">0xFF918f95</span>)),
                    ),
                  ),
                ),
              ),
            ),
          ),
          AnimatedAlign(
            alignment: themeProvider.isLightTheme
                <span style="color:#f92672">?</span> Alignment.centerLeft
                <span style="color:#f92672">:</span> Alignment.centerRight,
            duration: Duration(milliseconds: <span style="color:#ae81ff">350</span>),
            curve: Curves.ease,
            child: Container(
              alignment: Alignment.center,
              width: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">35</span>,
              height: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">13</span>,
              decoration: ShapeDecoration(
                  color: themeProvider.themeMode().toggleButtonColor,
                  shadows: themeProvider.themeMode().shadow,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">1</span>))),
              child: Text(
                themeProvider.isLightTheme
                    <span style="color:#f92672">?</span> widget.values[<span style="color:#ae81ff">0</span>]
                    <span style="color:#f92672">:</span> widget.values[<span style="color:#ae81ff">1</span>],
                style: TextStyle(
                    fontSize: width <span style="color:#f92672">*</span> .<span style="color:#ae81ff">045</span>, fontWeight: FontWeight.bold),
              ),
            ),
          )
        ],
      ),
    );
  }
}
</code></pre></div><h2 id="pubspecyaml-1"><strong>pubspec.yaml</strong></h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">dependencies:
  flutter:
    sdk: flutter
  hive: <span style="color:#f92672">^</span><span style="color:#ae81ff">1.4</span>.<span style="color:#ae81ff">1</span><span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>
  <span style="color:#960050;background-color:#1e0010">#</span>used to store local state
  path_provider: <span style="color:#f92672">^</span><span style="color:#ae81ff">1.6</span>.<span style="color:#ae81ff">5</span>
  provider: <span style="color:#f92672">^</span><span style="color:#ae81ff">4.3</span>.<span style="color:#ae81ff">1</span>
   <span style="color:#960050;background-color:#1e0010">#</span>used to manage global app state
  cupertino_icons: <span style="color:#f92672">^</span><span style="color:#ae81ff">0.1</span>.<span style="color:#ae81ff">3</span>
</code></pre></div><p>第二种方式 provider</p>
<h1 id="第六gex">第六gex</h1>
<h1 id="在-flutter-中使用-getx-动态更改您的应用主题">在 Flutter 中使用 GetX 动态更改您的应用主题</h1>
<p>在本文中，您将逐步学习如何在没有任何样板代码的情况下获得动态深色主题</p>
<h1 id="motivation">Motivation</h1>
<p>
        <a data-fancybox="gallery" href="https://miro.medium.com/max/375/1*1jKHA_05bXSK-uQ9-5C4JQ.gif">
            <img class="mx-auto" alt="img" src="https://miro.medium.com/max/375/1*1jKHA_05bXSK-uQ9-5C4JQ.gif" />
        </a>
    </p>
<h2 id="this-article-consists-of-two-parts">This article consists of two parts</h2>
<ul>
<li>
<h2 id="本文由两部分组成">本文由两部分组成</h2>
<ul>
<li>第 1 部分：如何获得动态暗模式</li>
<li>第 2 部分：如何使主题持久化</li>
</ul>
<p>此外，文章末尾还有一个示例项目。你可以检查一下</p>
</li>
</ul>
<h1 id="part-1-how-to-get-dynamic-dark-mode">Part 1: How to get dynamic dark mode</h1>
<h1 id="第-1-部分如何获得动态暗模式">第 1 部分：如何获得动态暗模式</h1>
<h2 id="第-1-步将包添加到您的-pubspecyaml-文件中">第 1 步：将包添加到您的 pubspec.yaml 文件中</h2>
<p><a href="https://pub.dev/packages/get">GetX</a>就像一个微<a href="https://pub.dev/packages/get">框架</a>，几乎可以为您完成所有工作。这次我们将使用它来将我们的应用程序主题更改为暗面。</p>
<pre><code>dependencies:
  get: ^3.13.2
</code></pre><h2 id="第-2-步创建明暗主题">第 2 步：创建明暗主题</h2>
<p>在这种情况下，我们只有 2 个主题。</p>
<pre><code>class Themes {
  static final light = ThemeData.light().copyWith(
    backgroundColor: Colors.white,
    buttonColor: Colors.blue,
  );
  static final dark = ThemeData.dark().copyWith(
    backgroundColor: Colors.black,
    buttonColor: Colors.red,
  );
}
</code></pre><h2 id="第-3-步将-materialapp-更改为-getmaterialapp-并添加主题">第 3 步：将 MaterialApp 更改为 GetMaterialApp 并添加主题</h2>
<p>GetX 给了我们更多的权力和功能来控制 MaterialApp，为了使用它们我们需要改变权力的来源</p>
<pre><code>return GetMaterialApp(
  ...
  theme: Themes.light,
  darkTheme: Themes.dark,
);
</code></pre><h2 id="第-4-步从主题中获取您的风格">第 4 步：从主题中获取您的风格</h2>
<p>您也可以使用<code>Theme.of(context)</code>代替<code>context.theme</code></p>
<pre><code>return Container(
  color: context.theme.backgroundColor
);
</code></pre><h2 id="第-5-步动态更改主题">第 5 步：动态更改主题</h2>
<p>我˚F ThemeMode是黑暗的模式，则是让它发光别的反之亦然</p>
<pre><code>onPressed: () {
  if (Get.isDarkMode)
    Get.changeThemeMode(ThemeMode.light);
  else
    Get.changeThemeMode(ThemeMode.dark);
}
</code></pre><p>就是这样！您的动态主题已准备就绪！现在我们可以开始下一部分。让主题持久化</p>
<h1 id="第-2-部分如何使主题持久化">第 2 部分：如何使主题持久化</h1>
<h2 id="第-1-步将包添加到您的-pubspecyaml-文件中-1">第 1 步：将包添加到您的 pubspec.yaml 文件中</h2>
<p><a href="https://pub.dev/packages/get_storage">get_storage</a>是 GetX 作者的本地存储包。它是<a href="https://pub.dev/packages/shared_preferences">shared_preferences</a>的替代方案，具有更好的性能。</p>
<pre><code>dependencies:
  flutter:
    sdk: flutter
  get: ^3.13.2
  get_storage: ^1.3.1 // add this
</code></pre><h2 id="第-2-步创建-themeservice">第 2 步：创建 ThemeService</h2>
<pre><code>class ThemeService {
  final _box = GetStorage();
  final _key = 'isDarkMode';
  
  /// Get isDarkMode info from local storage and return ThemeMode
  ThemeMode get theme =&gt; _loadThemeFromBox() ? ThemeMode.dark : ThemeMode.light;

  /// Load isDArkMode from local storage and if it's empty, returns false (that means default theme is light)
  bool _loadThemeFromBox() =&gt; _box.read(_key) ?? false;
  
  /// Save isDarkMode to local storage
  _saveThemeToBox(bool isDarkMode) =&gt; _box.write(_key, isDarkMode);
  
  /// Switch theme and save to local storage
  void switchTheme() {
    Get.changeThemeMode(_loadThemeFromBox() ? ThemeMode.light : ThemeMode.dark);
    _saveThemeToBox(!_loadThemeFromBox());
  }
}
</code></pre><h2 id="第-3-步初始化-getstorage-并从-themeservice-获取-thememode">第 3 步：初始化 GetStorage 并从 ThemeService 获取 ThemeMode</h2>
<p>我们初始化本地存储并从存储中获取主题信息</p>
<pre><code>void main() async { // make it async
  await GetStorage.init(); // add this
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      theme: Themes.light,
      darkTheme: Themes.dark,
      themeMode: ThemeService().theme, // add this
      home: HomePage(),
    );
  }
}
</code></pre><h2 id="第-4-步将-themeserviceswitchtheme-添加到按钮的-onpressed-方法中">第 4 步：将 ThemeService().switchTheme 添加到按钮的 onPressed 方法中</h2>
<pre><code>class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: context.theme.backgroundColor,
      body: Center(
        child: RaisedButton(
          color: context.theme.buttonColor,
          child: Text(
            'Change Theme',
            style: TextStyle(color: Colors.white),
          ),
          onPressed: ThemeService().switchTheme,
        ),
      ),
    );
  }
}
</code></pre><h2 id="example-project">Example Project</h2>
<p>If you’re the guy who better understands with source code. This is for you.</p>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://luckly.work/">luckly</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://luckly.work/post/flutter_tips/Flutter%E5%AE%9E%E7%8E%B0%E5%A4%9A%E4%B8%BB%E4%BD%93/">https://luckly.work/post/flutter_tips/Flutter%E5%AE%9E%E7%8E%B0%E5%A4%9A%E4%B8%BB%E4%BD%93/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/%E8%AF%BB%E4%B9%A6/%E8%B6%85%E7%BA%A7%E5%AD%A6%E4%B9%A0%E8%80%85%E7%9A%8410%E4%B8%AA%E4%B9%A0%E6%83%AF/">超级学习者的10个习惯</a></li>
        
        <li><a href="/post/flutter_web/Flutterer_web1%E4%B9%8B%E8%B7%AF%E7%94%B1%E6%90%BA%E5%B8%A6%E5%8F%82%E6%95%B0/">Flutterer_web1之路由携带参数</a></li>
        
        <li><a href="/post/%E8%AF%BB%E4%B9%A6/%E5%A6%82%E4%BD%95%E6%88%90%E4%B8%BA%E6%BC%94%E8%AE%B2%E9%AB%98%E6%89%8B/">如何成为演讲高手</a></li>
        
        <li><a href="/post/flutter_tips/Flutter%E9%9F%B3%E9%A2%91%E8%AF%86%E5%88%AB/">Flutter音频识别</a></li>
        
        <li><a href="/post/flutter_tips/Flutter%E4%B8%80%E5%88%87%E7%9A%86widget%E4%BD%86%E6%98%AF%E4%B8%8D%E8%A6%81%E5%B0%86%E6%89%80%E6%9C%89%E4%B8%9C%E8%A5%BF%E6%94%BE%E5%85%A5%E4%B8%80%E4%B8%AAwidget/">Flutter一切皆widget但是不要将所有东西放入一个widget</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/Flutter'>Flutter</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "https://github.com/ITmxs/repo"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://luckly.work/">早起的年轻人 By luckly</a>
        
        | <a rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">粤ICP备2021号-1</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://juejin.cn/user/3843548384077192" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">掘金</span></a>
        </div>
        <div class="github-badge">
            <a href="https://space.bilibili.com/480883651" target="_black"><span class="badge-subject">Design by</span><span class="badge-value bg-brightgreen">早起的年轻人</span></a>
        </div>
        <div class="github-badge">
            <a href="https://cloud.tencent.com/developer/user/6702670" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">云社区</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>
    <script src="/js/copy-to-clipboard.js"></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://luckly.work/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://luckly.work/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9/" title="Nginx文件内容">Nginx文件内容</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%9E%81%E7%AE%80%E6%95%99%E7%A8%8B/" title="Nginx极简教程">Nginx极简教程</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/%E5%8D%81%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8nginx/" title="十分钟入门Nginx">十分钟入门Nginx</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/go/Goland%E8%BF%9C%E7%A8%8B%E5%BC%80%E5%8F%91%E9%85%8D%E7%BD%AE/" title="Goland远程开发配置">Goland远程开发配置</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E8%AF%BB%E4%B9%A6/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" title="域名解析">域名解析</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/git/%E6%8F%90%E4%BA%A4%E8%BF%87%E7%A8%8B%E7%9C%81%E7%95%A5%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6/" title="提交过程省略某些文件">提交过程省略某些文件</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_DropdownButton%E7%A4%BA%E4%BE%8B/" title="Flutter_DropdownButton示例">Flutter_DropdownButton示例</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_ExpansionPanelList%E5%92%8CExpansionPanelList.radio%E7%A4%BA%E4%BE%8B/" title="Flutter_ExpansionPanelList和ExpansionPanelList">Flutter_ExpansionPanelList和ExpansionPanelList</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter%E5%BE%AE%E4%BF%A1%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E8%B7%B3%E5%9B%9EApp%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2/" title="Flutter微信分享链接跳回App指定页面">Flutter微信分享链接跳回App指定页面</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F/%E5%A4%96%E5%8C%85%E5%8F%AF%E8%83%BD%E7%9A%84%E9%97%AE%E9%A2%98/" title="外包可能的问题">外包可能的问题</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (326)</a></li>
    
    <li><a href="https://luckly.work/categories/IT/">IT (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Kotlin/">Kotlin (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Mysql/">Mysql (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (1)</a></li>
    
    <li><a href="https://luckly.work/categories/Vue/">Vue (6)</a></li>
    
    <li><a href="https://luckly.work/categories/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/android/">android (6)</a></li>
    
    <li><a href="https://luckly.work/categories/dart/">dart (96)</a></li>
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (28)</a></li>
    
    <li><a href="https://luckly.work/categories/gin/">gin (25)</a></li>
    
    <li><a href="https://luckly.work/categories/git/">git (4)</a></li>
    
    <li><a href="https://luckly.work/categories/Go/">Go (102)</a></li>
    
    <li><a href="https://luckly.work/categories/gorm/">gorm (4)</a></li>
    
    <li><a href="https://luckly.work/categories/grpc/">grpc (1)</a></li>
    
    <li><a href="https://luckly.work/categories/html/">html (3)</a></li>
    
    <li><a href="https://luckly.work/categories/ios/">ios (1)</a></li>
    
    <li><a href="https://luckly.work/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (6)</a></li>
    
    <li><a href="https://luckly.work/categories/python/">python (35)</a></li>
    
    <li><a href="https://luckly.work/categories/read/">读书笔记 (6)</a></li>
    
    <li><a href="https://luckly.work/categories/redis/">redis (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E5%8D%95/">书单 (8)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E8%AF%84/">书评 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师 (19)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师 (25)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E5%88%99/">原则 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%9F%BA%E7%A1%80/">基础 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%BF%83%E8%AF%AD/">心语 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%8F%92%E4%BB%B6/">插件 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%95%99%E5%AD%A6/">教学 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%B2%BE%E8%BF%9B/">精进 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%BC%96%E7%A8%8B/">编程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%99%9A%E5%B9%BB/">虚幻 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%91%E6%AD%A5/">跑步 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%BA%BF/">路线 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%A1%B9%E7%9B%AE/">项目 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%AD%85%E5%8A%9B/">魅力 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/IT/">IT</a>
    
    <a href="https://luckly.work/tags/Kotlin/">Kotlin</a>
    
    <a href="https://luckly.work/tags/Mysql/">Mysql</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/Vue/">Vue</a>
    
    <a href="https://luckly.work/tags/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载</a>
    
    <a href="https://luckly.work/tags/android/">android</a>
    
    <a href="https://luckly.work/tags/dart/">dart</a>
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/gin/">gin</a>
    
    <a href="https://luckly.work/tags/git/">git</a>
    
    <a href="https://luckly.work/tags/go/">go</a>
    
    <a href="https://luckly.work/tags/gorm/">gorm</a>
    
    <a href="https://luckly.work/tags/grpc/">grpc</a>
    
    <a href="https://luckly.work/tags/html/">html</a>
    
    <a href="https://luckly.work/tags/ios/">ios</a>
    
    <a href="https://luckly.work/tags/linux/">linux</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/python/">python</a>
    
    <a href="https://luckly.work/tags/redis/">redis</a>
    
    <a href="https://luckly.work/tags/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E5%8D%95/">书单</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E8%AF%84/">书评</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师</a>
    
    <a href="https://luckly.work/tags/%E5%85%A5%E9%97%A8/">入门</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E5%88%99/">原则</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯</a>
    
    <a href="https://luckly.work/tags/%E5%9F%BA%E7%A1%80/">基础</a>
    
    <a href="https://luckly.work/tags/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应</a>
    
    <a href="https://luckly.work/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程</a>
    
    <a href="https://luckly.work/tags/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路</a>
    
    <a href="https://luckly.work/tags/%E5%BF%83%E8%AF%AD/">心语</a>
    
    <a href="https://luckly.work/tags/%E6%8F%92%E4%BB%B6/">插件</a>
    
    <a href="https://luckly.work/tags/%E6%95%99%E5%AD%A6/">教学</a>
    
    <a href="https://luckly.work/tags/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明</a>
    
    <a href="https://luckly.work/tags/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书</a>
    
    <a href="https://luckly.work/tags/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单</a>
    
    <a href="https://luckly.work/tags/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫</a>
    
    <a href="https://luckly.work/tags/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸</a>
    
    <a href="https://luckly.work/tags/%E7%B2%BE%E8%BF%9B/">精进</a>
    
    <a href="https://luckly.work/tags/%E7%BC%96%E7%A8%8B/">编程</a>
    
    <a href="https://luckly.work/tags/%E8%99%9A%E5%B9%BB/">虚幻</a>
    
    <a href="https://luckly.work/tags/%E8%AF%97/">诗</a>
    
    <a href="https://luckly.work/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
    
    <a href="https://luckly.work/tags/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路</a>
    
    <a href="https://luckly.work/tags/%E8%B7%91%E6%AD%A5/">跑步</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%BA%BF/">路线</a>
    
    <a href="https://luckly.work/tags/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施</a>
    
    <a href="https://luckly.work/tags/%E9%80%9A%E8%AE%AF%E5%BD%95/">通讯录</a>
    
    <a href="https://luckly.work/tags/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学</a>
    
    <a href="https://luckly.work/tags/%E9%A1%B9%E7%9B%AE/">项目</a>
    
    <a href="https://luckly.work/tags/%E9%AD%85%E5%8A%9B/">魅力</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="http://www.topgoer.com/" title="枯藤">枯藤</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gorm.cn/zh_CN/docs/index.html" title="gorm">gorm</a>
        </li>
        
        <li>
            <a target="_blank" href="https://docs.python.org/zh-cn/3/tutorial/index.html" title="python">python</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.liwenzhou.com/" title="李文周">李文周的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://www.xbzweb.com/" title="小包子的博客">小包子的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.flysnow.org/" title="飞雪无情的博客">飞雪无情的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://sliverhorn.com/" title="sliverhorn的博客">sliverhorn的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gesdh.cn/" title="小格子">格子导航</a>
        </li>
        
        <li>
            <a target="_blank" href="https://itachi.xyz/" title="阿林">itachi&#39;s Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://darjun.github.io/" title="大俊">大俊Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://geektutu.com/post/quick-golang.html" title="极客兔兔">极客兔兔Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="http://zxfcumtcs.github.io/" title="赵雪峰">雪峰Blog</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://luckly.work/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>